﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Data;


public class DataRowEx : IEnumerable
{
    // Fields
    private Dictionary<string, object> dic;
    private DataTable dt;
    private string p;
    private string p_2;

    // Methods
    public DataRowEx()
    {
        dic = new Dictionary<string, object>();
    }

    public DataRowEx(Dictionary<string, object> data)
    {
        dic = data;
    }

    public DataRowEx(DataRow dr)
    {
        if (dic == null) dic = new Dictionary<string, object>();
        //
        foreach (DataColumn col in dr.Table.Columns)
        {
            if (!dic.ContainsKey(col.ColumnName.ToUpper()))
            {
                dic.Add(col.ColumnName.ToUpper(), dr[col]);
            }
            else
            {
                dic[col.ColumnName.ToUpper()] = dr[col];
            }
        }
    }

    public DataRowEx(IDataReader rd)
    {
        if (dic == null) dic = new Dictionary<string, object>();
        //
        while (rd.Read())
        {
            for (int i = 0; i < rd.FieldCount; i++)
            {
                if (dic.ContainsKey(rd.GetName(i).ToUpper()))
                    dic.Add(rd.GetName(i).ToUpper(), rd.GetValue(i));
            }
        }
        if (!rd.IsClosed)
            rd.Close();
    }

    public DataRowEx(DataTable dt, string p, string p_2)
    {
        // TODO: Complete member initialization
        this.dt = dt;
        this.p = p;
        this.p_2 = p_2;
    }

    //public DataRowEx(DataTable dt, string keyField, string valueField)
    //{
    //    foreach (DataRow dr in dt.Rows)
    //    {
            
    //    }
    //}

    public void Add(string key, object value)
    {
        if (!dic.ContainsKey(key))
        {
            dic.Add(key, value);
        }
        else
            dic[key] = value;
    }

    public static DataRowEx Deserialize(byte[] byteArray)
    {
        System.IO.MemoryStream memStream = new System.IO.MemoryStream(byteArray);
        System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(DataRowEx));
        DataRowEx drEx = (DataRowEx)serializer.Deserialize(memStream);
        return drEx;
    }

    public IEnumerator GetEnumerator()
    {
        return (IEnumerator)dic.ToList();
    }

    public T GetValue<T>(int iColIndex)
    {
        return Globals.Object_GetValue<T>(Item(iColIndex));
    }

    public T GetValue<T>(string sColName)
    {
        return Globals.Object_GetValue<T>(Item(sColName));
    }

    public T GetValue<T>(int iColIndex, T nullValue)
    {
        T retVal = Globals.Object_GetValue<T>(Item(iColIndex));
        if (Null.IsNull(retVal))
            return nullValue;
        else
            return retVal;
    }

    public T GetValue<T>(string sColName, T nullValue)
    {
        T retVal = Globals.Object_GetValue<T>(Item(sColName));
        if (Null.IsNull(retVal))
            return nullValue;
        else
            return retVal;
    }

    public bool HasItem(string sColName)
    {
        return dic.ContainsKey(sColName.ToUpper());
    }

    public object Item(int iColIndex)
    {
        return dic.ToList()[iColIndex].Value;
    }

    public object Item(string sColName)
    {
        if (dic.ContainsKey(sColName.ToUpper()))
            return dic[sColName.ToUpper()];
        else
            return null;
    }

    public static byte[] Serialize(DataRowEx dataRowEx)
    {
        if (dataRowEx == default(DataRowEx))
            return default(byte[]);

        System.IO.MemoryStream memStream = new System.IO.MemoryStream();
        System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(DataRowEx));
        serializer.Serialize(memStream, dataRowEx);

        return memStream.ToArray();
    }

    // Properties
    public Dictionary<string, object> Data
    {
        get
        {
            return dic;
        }
    }

    public bool HasData
    {
        get
        {
            return Data.Keys.Count > 0;
        }
    }

    //public object this[string sColName]
    //{
    //    get
    //    {
    //        return dic[sColName];
    //    }
    //}

    //public object this[int iColIndex]
    //{
    //    get
    //    {
    //        return dic.ToList()[iColIndex];
    //    }
    //}
}

